package com.hymjweb.demo.hymj.customer_manage.freight_agreement.auto.controller;

import com.alibaba.fastjson.JSONObject;
import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.customer_manage.freight_agreement.bean.AgrementRatePojo;
import com.hymjweb.demo.hymj.customer_manage.freight_agreement.auto.bean.BCustomerAutoFreightAgreementPojo;
import com.hymjweb.demo.hymj.customer_manage.freight_agreement.auto.service.BCustomerAutoFreightAgreementService;
import com.hymjweb.demo.hymj.customer_manage.freight_agreement.detail.bean.BCustomerFeightAgreementDetailPojo;
import com.hymjweb.demo.hymj.customer_manage.freight_agreement.detail.service.BCustomerFeightAgreementDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/pages/customer_manage/freight_agreement/auto")
public class BCustomerAutoFreightAgreementController extends BaseController {

    @Autowired
    private BCustomerAutoFreightAgreementService bCustomerAutoFreightAgreementService;

    @Autowired
    private BCustomerFeightAgreementDetailService bCustomerFeightAgreementDetailService;

    @RequestMapping("/showAutoFreightAgreement")
    public String showAutoFreightAgreement() {
        return "/pages/customer_manage/freight_agreement/auto/autoFreightAgreementList";
    }

    @RequestMapping("/queryAutoFreightAgreementList")
    @ResponseBody
    public Object queryAutoFreightAgreementList(@RequestParam(required = false) Map param) throws Exception {
        return bCustomerAutoFreightAgreementService.getPageList(param);
    }


    @RequestMapping("/saveAutoAgreement")
    @ResponseBody
    public Object saveAutoAgreement(@RequestBody(required = true) JSONObject agreementPojo) throws Exception {
        if (agreementPojo == null) {
            throwException("保存汽运协议参数错误");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("保存汽运协议运价成功!");

        String opFlag = DataTypeUtil.toStringObject(agreementPojo.get("opFlag"));

        // 1. 得到b_customer_auto_freight_agreement对象与 b_customer_feight_agreement_detail 列表对象
        BCustomerAutoFreightAgreementPojo autoAgreementPojo = bCustomerAutoFreightAgreementService.convertToJavaBean(agreementPojo);

        if (opFlag.equals(SysConstant.OPER_ADD)) {
            int effectRow = bCustomerAutoFreightAgreementService.addAutoFreightAgreement(autoAgreementPojo);
            if (effectRow == SysConstant.EXIST_STATUS.EXIST.getId()) {
                baseResultMap.setOk(false);
                baseResultMap.setMessage("保存汽运协议运价失败，需要添加协议运价对象已经存在，不能重复添加!");
            } else if (effectRow <= SysConstant.INT_ZERO) {
                baseResultMap.setOk(false);
                baseResultMap.setMessage("保存汽运协议运价失败!");
            }
        } else {
            // 这是编辑保存
            int effectRow = bCustomerAutoFreightAgreementService.updateAutoFreightAgreement(autoAgreementPojo);
            baseResultMap.setOk(false);
            baseResultMap.setMessage("更新汽运协议运价失败!");
        }
        return baseResultMap;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody(required = true) JSONObject idsJsonObject) throws Exception {
        if (idsJsonObject == null) {
            throwException("协议运价删除参数错误！");
        }

        String agreementFreightIds = DataTypeUtil.toStringObject(idsJsonObject.get("agreementFreightIds"));

        if (agreementFreightIds == null) {
            throw new Exception("协议运价删除参数错误！");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("协议运价删除成功!");

        int effectRow = bCustomerAutoFreightAgreementService.delete(agreementFreightIds);

        if (effectRow <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("协议运价删除失败，请重试!");
        }
        return baseResultMap;

    }

    @RequestMapping("/upload")
    @ResponseBody
    public Object upload(MultipartFile upLoadFileData) throws Exception {
        if (upLoadFileData == null) {
            throwException("运价文件获取参数错误，上传失败！");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("运价文件上传成功!");

        int effectRow = bCustomerAutoFreightAgreementService.upload(upLoadFileData);

        if (effectRow <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("运价文件上传失败!");
        }
        return baseResultMap;
    }

    @RequestMapping("/loadAirAgrement")
    @ResponseBody
    public Object loadAirAgrement(@RequestBody(required = true) Map param) throws Exception {

        if (param == null || param.isEmpty()) {
            throwException("加载费率参数异常！");
        }

        AgrementRatePojo ratePojo = bCustomerAutoFreightAgreementService.loadAirAgrementRatePojo(param);

        BaseResultMap baseResultMap = new BaseResultMap();
        if(ratePojo==null){
            baseResultMap.setOk(false);
            baseResultMap.setMessage("根据【客户名称】【目的市】【货物类型】【计算重量】【体积】检索不到客户费率!");
        }
        else if (ratePojo.getWeightRate() == 0 && ratePojo.getVolumeRate() == 0) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("根据【客户名称】【目的市】【货物类型】【计算重量】【体积】检索不到客户费率!");
        } else {
            baseResultMap.setOk(true);
            baseResultMap.setMessage("根据【客户名称】【目的市】【货物类型】【计算重量】【体积】成功检索到客户费率!");
        }
        baseResultMap.setData(ratePojo);

        return baseResultMap;
    }

    @RequestMapping("/showAutoAgreementDetail")
    @ResponseBody
    public List<BCustomerFeightAgreementDetailPojo> showAutoAgreementDetail(@RequestBody(required = true) Map param) throws Exception {

        /**
         * let airWeightUrl = baseUrl + "standardTypeId=" + standardType.weight;
         *             airWeightUrl += "&customerId=" + deliveryCustomerIdValue;
         *             airWeightUrl += "&cityId=" + destCityIdValue;
         */
        if (param == null || param.isEmpty()) {
            throwException("加载汽运费率参数异常！");
        }

        BCustomerAutoFreightAgreementPojo pojo = bCustomerAutoFreightAgreementService.getPojoByUk(param);
        if (pojo == null) {
            return null;
        }
        List<BCustomerFeightAgreementDetailPojo> list = bCustomerFeightAgreementDetailService.getDetailListByAgreementFreightId(pojo.getId());
        return list;
    }

}
